/**
 * All rights Reserved, Designed By 溪云阁
 * Copyright:    Copyright(C) 2016-2020
 */

package com.boots.mp.view.dict.view;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.boots.mp.business.dict.domain.Dict;
import com.boots.mp.business.dict.domain.GetDictListBO;
import com.boots.mp.business.dict.service.IDictService;
import com.boots.mp.view.dict.vo.GetDictListVO;
import com.boots.mp.view.dict.vo.GetDictVO;
import com.module.boots.api.message.ResponseMsg;
import com.module.boots.api.utils.MsgUtils;
import com.module.boots.exception.CommonRuntimeException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;

/**
 * 数据字典接口
 * @author：溪云阁
 * @date：2020年5月10日
 */
@SuppressWarnings("deprecation")
@Api(tags = { "后台系统服务：数据字典接口" })
@RestController
@RequestMapping("view/dict")
public class DictView {

    @Autowired
    private IDictService dictService;

    /**
     * 获取数据字典数据
     * @author 溪云阁
     * @param id
     * @return ResponseMsg<GetDictVO>
     */
    @ApiOperation(value = "获取数据字典数据")
    @GetMapping(value = "/getDict", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SneakyThrows(CommonRuntimeException.class)
    public ResponseMsg<GetDictVO> getDict(@RequestParam("id") Long id) {
        final Dict dict = dictService.getById(id);
        final GetDictVO vo = GetDictVO.builder()
                .id(dict.getId())
                .name(dict.getName())
                .value(dict.getValue())
                .description(dict.getDescription())
                .build();
        return MsgUtils.buildSuccessMsg(vo);
    }

    /**
     * 联合查询列表数据
     * @author 溪云阁
     * @param name
     * @return ResponseMsg<GetDictVO>
     */
    @ApiOperation(value = "联合查询列表数据")
    @GetMapping(value = "/getDictList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SneakyThrows(CommonRuntimeException.class)
    public ResponseMsg<List<GetDictListVO>> getDictList(@RequestParam("name") String name) {
        final List<GetDictListBO> bos = dictService.getDictList(name);
        final List<GetDictListVO> vos = new ArrayList<>();
        bos.forEach(bo -> {
            final GetDictListVO vo = GetDictListVO.builder()
                    .id(bo.getId())
                    .name(bo.getName())
                    .typeName(bo.getTypeName())
                    .value(bo.getValue())
                    .description(bo.getDescription())
                    .build();
            vos.add(vo);
        });
        return MsgUtils.buildSuccessMsg(vos);
    }

}
